package org.linlinjava.litemall.admin.config;

import java.util.*;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.linlinjava.litemall.admin.shiro.AdminAuthorizingRealm;
import org.linlinjava.litemall.admin.shiro.AdminMobOremailRealm;
import org.linlinjava.litemall.admin.shiro.AdminWebSessionManager;
import org.linlinjava.litemall.admin.shiro.TokenAuthenticationFilter;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import javax.servlet.Filter;
@Configuration
public class ShiroConfig {
    @Bean
    public Realm adminAuthorizingRealm() {
        AdminAuthorizingRealm adminAuthorizingRealm = new AdminAuthorizingRealm();
        adminAuthorizingRealm.setCachingEnabled(true);
        //只缓存 登陆的token
        adminAuthorizingRealm.setAuthenticationCachingEnabled(true);
        //不缓存 权限信息 换成在service 层控制缓存
        adminAuthorizingRealm.setAuthorizationCachingEnabled(false);
        return adminAuthorizingRealm;
    }

    @Bean
    public Realm adminMobOremailRealm() {
        AdminMobOremailRealm adminMobOremailRealm = new AdminMobOremailRealm();
        adminMobOremailRealm.setCachingEnabled(true);
        //只缓存 登陆的token
        adminMobOremailRealm.setAuthenticationCachingEnabled(true);
        //不缓存 权限信息 换成在service 层控制缓存
        adminMobOremailRealm.setAuthorizationCachingEnabled(false);
        return adminMobOremailRealm;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        Map<String, Filter> filters = new LinkedHashMap<>();
        filters.put("tokenAuth", new TokenAuthenticationFilter()); // 注册自定义的Token验证过滤器


        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/admin/auth/login", "anon");
        filterChainDefinitionMap.put("/admin/auth/captcha", "anon");
        filterChainDefinitionMap.put("/admin/auth/mobOremailLogin", "anon");
        filterChainDefinitionMap.put("/admin/auth/401", "anon");
        filterChainDefinitionMap.put("/admin/auth/index", "anon");
        filterChainDefinitionMap.put("/admin/auth/403", "anon");
        filterChainDefinitionMap.put("/admin/index/index", "anon");
        filterChainDefinitionMap.put("/admin/storage/track", "anon");
        filterChainDefinitionMap.put("/admin/storage/fetch/**", "anon");
        filterChainDefinitionMap.put("/admin/storage/download/**", "anon");
        filterChainDefinitionMap.put("/admin/version/download/**", "anon");

        // 需要验证token的接口路径
        //filterChainDefinitionMap.put("/admin/**", "tokenAuth");

        //shiroFilterFactoryBean.setFilters(filters);
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        shiroFilterFactoryBean.setLoginUrl("/admin/auth/401");
        shiroFilterFactoryBean.setSuccessUrl("/admin/auth/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/admin/auth/403");

        return shiroFilterFactoryBean;
    }

    @Bean
    public SessionManager sessionManager() {
        return new AdminWebSessionManager();
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        List<Realm> realms = new ArrayList<>();
        realms.add(adminMobOremailRealm());
        realms.add(adminAuthorizingRealm());
        securityManager.setRealms(realms);
        securityManager.setSessionManager(sessionManager());
        return securityManager;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor =
                new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public static DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
        return creator;
    }

}
